package com.tang.client;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.charset.StandardCharsets;

public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {

        // 基于符号换行
        socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024));
        // 解码Buffer转String，注意调整自己的编码格式GBK、UTF8
        socketChannel.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8));
        // 编码String转换Buffer，注意调整自己的编码格式GBK、UTF8
        socketChannel.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
        // 在管道中添加我们自己的接收数据实现方法
        socketChannel.pipeline().addLast(new MyClientHandler());
        
    }

}
